$(function() {
    ajax2();
    ajax();
    $("#change").click(function() {
        var stuAge = $("#stuAge").val();
        var stuQQ = $("#stuQQ").val();
        var stuPhone = $("#stuPhone").val();
        var stuClassid = ($("#classID-select").val());
        var stuaddress = $("#stuAddress").val();
        $.ajax({
            url: "/blackhouse/studentinfo",
            type: "post",
            data: { "code": 2, "stuAge": stuAge, "stuQQ": stuQQ, "stuPhone": stuPhone, "stuclassid": stuClassid, "stuAddress": stuaddress },
            dataType: "json",
            success: function(e) {
                if (e) {
                    swal('修改成功', '欢迎使用', 'success');
                    ajax();
                } else {
                    swal('修改失败', '未知错误', 'erreo');
                }
            }
        })
    })
})

function ajax() {
    $.ajax({
        url: "/blackhouse/studentinfo",
        type: "post",
        data: { "code": 0 },
        dataType: "json",
        success: function(e) {
            $("#stuName").val(e[0].stuName);
            $("#stuAge").val(e[0].stuAge);
            if (e[0].stuSex == "男") {
                $("#sex1").prop("checked", true);
                $("#sex2").prop("checked", false);
            } else {
                $("#sex2").prop("checked", true);
                $("#sex1").prop("checked", false);
            }
            $("#stuQQ").val(e[0].stuQQ);
            $("#stuPhone").val(e[0].stuPhone);
            $("#classID-select option").each(function(){
        		if($(this).text() == e[0].stuclassid){
        			$(this).attr("selected","selected");
        		}
        	});

            $("#stuAddress").val(e[0].stuAddress);
            $("#stuCode").val(e[0].stuCode);
        }
    })
}

function ajax2() {
    $.ajax({
        url: "/blackhouse/seeclass",
        type: "post",
        dataType: "json",
        success: function(e) {
            var html = "";
            for (var i = 0; i < e.length; i++) {
            	html += "<option value='" + e[i].id + "'>" + e[i].className
				+ "</option>"
            }
            $("#classID-select").html(html);
        }
    })
}